package com.drought.rained.presenter;

import android.annotation.SuppressLint;

import com.drought.rained.network.ApiServiceLJ;
import com.drought.rained.business.CustomConsumerLJ;
import com.drought.rained.network.RetrofitHelperLJ;
import com.drought.rained.response.BaseResponseLJ;
import com.drought.rained.response.BlankResponseLJ;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

/**
 * Created by zhanglibo on 2018/1/20.
 */

@SuppressLint("CheckResult")
public class BasePresenterLJ {
    protected RetrofitHelperLJ helper;
    protected Retrofit mRetrofit;
    protected ApiServiceLJ req;

    public BasePresenterLJ() {
        helper = RetrofitHelperLJ.getRetorfitHelper();
        mRetrofit = helper.getRetrofitClient();
        req = mRetrofit.create(ApiServiceLJ.class);
    }

    public <T extends BaseResponseLJ> void toSubScribe(Observable<T> o, CustomConsumerLJ<T> c, Consumer<Throwable> e) {
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(c, e);
    }

    public <T> void toSubScribe(List<Observable<? extends Object>> o, Consumer<BlankResponseLJ> c, Consumer<Throwable> e) {
        Observable.zip(o, new Function<Object[], BlankResponseLJ>() {
            @Override
            public BlankResponseLJ apply(Object[] objects) throws Exception {
                return null;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(c, e);
    }
}
